package com.battle.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.UserScreen;
import com.battle.pojo.param.userScreen.*;
import com.battle.pojo.vo.userScreen.*;
import com.battle.pojo.dto.userScreen.*;
import com.battle.service.repository.in.UserScreenRepository;
import com.battle.service.service.in.UserScreenService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：用户筛选框服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class UserScreenServiceImpl implements UserScreenService {

    private final UserScreenRepository userScreenRepository;

    public UserScreenServiceImpl(UserScreenRepository userScreenRepository) {
        this.userScreenRepository = userScreenRepository;
    }


    /**
     * 创建用户筛选框
     *
     * @param param
     */
    @Override
    public void createUserScreen(CreateUserScreenParam param) {
        UserScreen userScreen = new UserScreen();
        BeanUtils.copyProperties(param, userScreen);
        Long id = IdGenerator.SNOW.generate();
        userScreen.setId(id);
        userScreenRepository.create(userScreen);
    }

    /**
     * 修改用户筛选框
     *
     * @param param
     */
    @Override
    public void editUserScreen(EditUserScreenParam param) {
        Long id = param.getId();
        UserScreen userScreen = userScreenRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, userScreen);
        userScreenRepository.edit(userScreen);
    }

    /**
     * 移除用户筛选框
     *
     * @param id
     */
    @Override
    public void removeUserScreen(Long id) {
        userScreenRepository.removeById(id);
    }

    /**
     * 启动/禁用用户筛选框
     *
     * @param id
     */
    @Override
    public void enabledUserScreen(Long id) {
        UserScreen userScreen = userScreenRepository.getByIdCheck(id);
        userScreen.setIsDisable(!userScreen.getIsDisable());
        userScreenRepository.edit(userScreen);
    }

    /**
     * 获取用户筛选框详情
     *
     * @param id
     * @return
     */
    @Override
    public GetUserScreenVo getUserScreen(Long id) {
        UserScreen userScreen = userScreenRepository.getByIdCheck(id);
        GetUserScreenVo vo = new GetUserScreenVo();
        BeanUtils.copyProperties(userScreen, vo);
        return vo;
    }

    /**
     * 获取用户筛选框 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetUserScreenListVo> getUserScreenList(GetUserScreenListParam param) {

        SelectUserScreenDto dto = new SelectUserScreenDto();
        BeanUtils.copyProperties(param, dto);
        Page<UserScreen> page = userScreenRepository.selectUserScreen(dto);
        List<UserScreen> userScreenList = page.getRecords();
        List<GetUserScreenListVo> list = new ArrayList<>();
        for (UserScreen userScreen : userScreenList) {
            GetUserScreenListVo vo = new GetUserScreenListVo();
            BeanUtils.copyProperties(userScreen, vo);
            vo.setEnable(!userScreen.getIsDisable());
            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取用户筛选框下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetUserScreenDownListVo> getUserScreenDownList(GetUserScreenDownListParam param) {
        UserScreen po = new UserScreen();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<UserScreen> userScreenList = userScreenRepository.getListByEntity(po);

        List<GetUserScreenDownListVo> list = new ArrayList<>();
        for (UserScreen userScreen : userScreenList) {
            GetUserScreenDownListVo vo = new GetUserScreenDownListVo();
            BeanUtils.copyProperties(userScreen, vo);
            vo.setEnable(!userScreen.getIsDisable());
            list.add(vo);
        }
        return list;
    }


}



